package titt.util.deploy;

import titt.service.content.Mimes;
import titt.util.Bytes;
import titt.util.io.compile.JSCompiler;
import titt.util.io.loader.FileStream;

public class CompilerCache implements Cache {

	private FileStream fl;
	private byte[] val;
	private long modified = -1;
	
	public CompilerCache(FileStream fl) {
		this.fl = fl;
	}
	
	@Override
	public String getMimeType() {
		return Mimes.JAVASCRIPT;
	}

	@Override
	public byte[] getValue() {
		return val;
	}

	@Override
	public boolean isOutdated() {
		return modified < fl.lastModified();
	}

	@Override
	public void update() throws Exception {
		modified = fl.lastModified();
		val = Bytes.asBytes(JSCompiler.compileString(Bytes.asString(fl.readFile())));
	}

}
